<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container justify-between">
      <div>
        <el-switch v-model="multi" inactive-text="批量操作"></el-switch>
        <template v-if="multi">
          <el-button size="mini" type="primary" @click="doExportExcel()">导出花名册</el-button>
          <el-button size="mini" type="info" @click="noticeMianshi()">通知面试</el-button>
          <el-button size="mini" type="info" @click="noticeTrain()">通知培训</el-button>
          <el-button size="mini" type="danger" @click="doRemove('','确定要删除吗？')">删除</el-button>
        </template>
      </div>
      <div class="overflow_60">
        <el-select @change='doSearch' style="width: 150px" class="filter-item" v-model="listQuery.dept_id"
                   placeholder="全部部门">
          <el-option  label="全部部门" value=""></el-option>
          <el-option v-for="(item,index) in dept_list" :key="item.id" :label="item.name"
                     :value="item.id"></el-option>
        </el-select>
        <el-select @change='doSearch' style="width: 150px" class="filter-item" v-model="listQuery.job_id"
                   placeholder="全部岗位">
          <el-option  label="全部岗位" value=""></el-option>
          <el-option v-for="(item,index) in job_list" :key="item.id" :label="item.name"
                     :value="item.id"></el-option>
        </el-select>
        <el-select @change='doSearch' style="width: 120px" class="filter-item" v-model="listQuery.status"
                   placeholder="全部状态">
          <el-option  label="全部状态" value=""></el-option>
          <el-option v-for="(item,index) in status_list" :key="item.data_value" :label="item.data_text"
                     :value="item.data_value"></el-option>
        </el-select>
        <el-select @change='doSearch' style="width:120px" class="filter-item" v-model="listQuery.gender"
                   placeholder="全部性别">
          <el-option  label="全部性别" value=""></el-option>
          <el-option v-for="(item,index) in gender_list" :key="item.data_value" :label="item.data_text"
                     :value="item.data_value"></el-option>
        </el-select>
        <el-input @keyup.enter.native="doSearch" style="width: 120px;" class="filter-item" placeholder="最小年龄"
                  v-model="listQuery.start_age">
        </el-input>
        <el-input @keyup.enter.native="doSearch" style="width: 120px;" class="filter-item" placeholder="最大年龄"
                  v-model="listQuery.end_age">
        </el-input>
        <el-input @keyup.enter.native="doSearch" style="width: 200px;" class="filter-item" placeholder="输入名称搜索"
                  v-model="listQuery.keyword">
        </el-input>
        <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="doSearch">搜索
        </el-button>
      </div>
    </div>
    <el-table ref="multipleTable" :data="list" v-loading="listLoading" element-loading-text="给我一点时间"
              border fit highlight-current-row style="width: 100%" @selection-change="onSelectionChange"
              height="600">
      <el-table-column v-if='multi' type="selection" width="55"></el-table-column>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="left" label="岗位" width="200px">
        <template slot-scope="scope">
          <span>【{{scope.row.dept_name}}】{{(scope.row.job_name=="通用岗位")?"---":scope.row.job_name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="应聘人" width="80">
        <template slot-scope="scope">
          <span class="relative_span">{{scope.row.name}} <i class='position_black' v-if="scope.row.black_flag==1">黑</i></span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="手机号" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="性别" width="60">
        <template slot-scope="scope">
          <span>{{scope.row.gender | show_basedatas('gender','---')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 年龄"  width="60">
        <template slot-scope="scope">
          <span>{{scope.row.birthday | showFixedAge('---')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 登记表" width="130">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="scanResume(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 登记时间" width="130">
        <template slot-scope="scope">
          <span>{{scope.row.create_time | beautiful_time}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 入职时间" width="130">
        <template slot-scope="scope">
          <span>{{scope.row.ruzhi_time | beautiful_time}}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 面试结果" v-if="company.modules&&company.modules.indexOf('interview')!=-1" width="200">
        <template slot-scope="scope">
          <span v-if="scope.row.interviews">
            <a  :class="'block_status status_'+item.result" v-for="item in scope.row.interview_bean" @click="showInterviewWin(item.interview_id,scope.row.interview_detail)">
            {{item.step}}面
              <i v-if="item.result==1" class='iconfont icon-duigou'></i>
              <i v-if="item.result==-1" class='iconfont icon-cuohao'></i>
            </a>
          </span>
          <span v-else>---</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 培训结果" v-if="company.modules&&company.modules.indexOf('interview')!=-1">
        <template slot-scope="scope">
          <!--<a   class='block_status status_1'>-->
            <el-tag v-if="scope.row.train_flag==2" type="success">已培训</el-tag>
          <!--</a>-->
          <span v-else>---</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 供应商">
        <template slot-scope="scope">
          <span v-if="scope.row.referrer_id_name&&bean.referrer_id_name!='null'">{{scope.row.referrer_id_name }}</span>
          <span v-else-if="scope.row.supplier_name&&bean.supplier_name!='null'">{{scope.row.supplier_name }}</span>
          <span v-else-if="scope.row.channel_source&&bean.channel_source!='null'">{{scope.row.channel_source }}</span>
          <span v-else>---</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label=" 离职日期">
        <template slot-scope="scope">
          <span>{{scope.row.lizhi_time  | beautiful_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="left" label="当前状态">
        <template slot-scope="scope">
          <span>{{scope.row.status  | show_basedatas('dengji_status','---') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="操作" class-name="small-padding fixed-width" fixed="right" width="300">
        <template slot-scope="scope">
          <el-button round size="mini" type="primary" @click="downloadResume(scope.row.id)">下载简历</el-button>
          <el-button round size="mini" type="danger" v-if="scope.row.black_flag!=1"
                     @click="doPullBlack(scope.row.mobile)">拉黑
          </el-button>
          <el-tag type="info" v-if="scope.row.mianshi_flag==2">已通知面试</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="onSizeChange" @current-change="onCurrentChange"
                     :current-page="listQuery.pageNumber" :page-sizes="[12,50,100,10000]"
                     :page-size="listQuery.pageSize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <mianshiWin @refreshList="loadList"  @noticeWinClose="noticeWinClose" v-if="showMianshi" :ids="ids" :mobiles="mobiles"></mianshiWin>
    <resumeTemplate v-if="showResumeTemplate" :id="resume_dengji_id" :template="template" @resumeWinClose="resumeWinClose"></resumeTemplate>
    <interviewWin @interviewWinClose="interviewWinClose" v-if="showInterview" :interview_data="interview_data"></interviewWin>
  </div>
</template>
<script>
  import Vue from 'vue'
  import waves from '@/directive/waves' // 水波纹指令
  import API from '@/api'
  import Http from '@/http'
  import Store from '@/store'
  import Msg from '@/utils/msg'
  import Utils from '@/utils/common'
  import _ from 'underscore'
  import Qiniu from '@/utils/qiniu'
  import {mapGetters} from 'vuex'
  import mianshiWin from '@/components/mianshiWin'
  import resumeTemplate from '@/components/resumeTemplate'
  import interviewWin from '@/components/interviewWin'

  let Base64 = require('js-base64').Base64;
  export default {
    directives: {waves},
    data() {
      return {
        dialogTitle:'',
        dialogFormVisible:false,
        dealing: false,
				multi: true,
        company:Store.getters.user.company,
        list: [],
        total: 0,
        listLoading: true,
        listQuery: {
          pageNumber: 1,
          pageSize: 12,
          keyword: '',
          status: '',
          dept_id:'',
          job_id:'',
          gender:'',
          start_age:'',
          end_age:'',
        },
        dept_list: [],
        job_list:[],
        status_list:Store.getters.basedata.dengji_status,
        gender_list:Store.getters.basedata.gender,
        bean: {},
        multipleSelection: [],
        showMianshi:false,
        mobiles:'',
        ids:'',
        showResumeTemplate:false,
        resume_dengji_id:'',
        template:'',
        showInterview:false,
        interview_data:{},
      }
    },
    filters: {},
    components: {
      mianshiWin,resumeTemplate,interviewWin
    },
    created() {
      this.loadList()
      this.loadDeptList()
      this.loadJobList()
    },
    computed: {
    },
    methods: {

      loadJobList(){
        Http.getModel(API.get_company_job_list, {}, false).then((json) => {
          this.loading = false
          if (json.code == 0) {
            this.job_list = json.data.list
          }
        })
      },
      loadList() {
        this.listLoading = true
        Http.getModel(API.get_dengji_list, this.listQuery, false).then((json) => {
          this.loading = false
          if (json.code == 0) {
            var list = json.data.list
            for(let i=0;i<list.length;i++){
              if(list[i].interviews){
                list[i].interview_bean = []
                var str2 = list[i].interviews;
                var str1 = list[i].interview_detail;
                if(str2&&str2!='null'){
                  var arr = str2.split(',')
                  var arr_detail = JSON.parse(str1)||[];
                  var return_arr=[];
                  for(let i=0;i<arr.length;i++){
                    if(arr_detail[i]){
                      return_arr.push(arr_detail[i])
                    }else{
                      var item = {
                        interview_id :arr[i],
                        result:0,
                        step:i+1
                      }
                      return_arr.push(item)
                    }
                  }
                  list[i].interview_bean = return_arr
                }
              }
            }
            this.list = list
            this.total = parseInt(json.data.total)
            this.listLoading = false
          }
        })
      },
      loadDeptList() {
        Http.getModel(API.get_company_dept_list, {}, false).then((json) => {
          this.loading = false
          if (json.code == 0) {
            this.dept_list = json.data.list
          }
        })
      },
      loadBean(id) {
        Http.getModel(API.get_interview, {id: id}).then((json) => {
          if (json.code == 0) {
            let bean = json.data
            this.bean = bean
          }
        })
      }
      ,
      saveBean() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dealing = true
            this.bean.pwd = Base64.encode(this.bean.login_pwd);
            Http.getModel(API.save_interview, this.bean, false).then((json) => {
              this.dealing = false
              if (json.code == 0) {
                this.dialogFormVisible = false
                Msg.success(json.msg, () => {
                  this.loadList()
                })
              } else {
                Msg.error(json.msg)
              }
            })
          }
        })
      }
      ,
      doRemove(ids) {
        if (!ids) {
          if (this.multipleSelection.length == 0) {
            Msg.error("请先选择");
            return false;
          } else {
            ids = _.pluck(this.multipleSelection, "id").join(",");
          }
        }

        Msg.prompt('请输入安全密码', (pwd) => {
          Http.getModel(API.remove_worker, {ids: ids,pwd:Base64.encode(pwd)}, false).then((json) => {
            if (json.code == 0) {
              Msg.success(json.msg, () => {
                this.loadList()
              })
            } else {
              Msg.error(json.msg)
            }
          })
        })
      }
      ,
      doPullBlack(mobiles){
        Msg.prompt('请输入拉黑原因', (reason) => {
          Http.getModel(API.add_to_black_list, {mobile: mobiles,reason:reason}, false).then((json) => {
            if (json.code == 0) {
              Msg.success(json.msg, () => {
                this.loadList()
              })
            } else {
              Msg.error(json.msg)
            }
          })
        })
      },
      /***   公用部分 start ***/
      onSelectionChange(val) {
        this.multipleSelection = val;
      }
      ,
      doSearch() {
        this.listQuery.pageNumber = 1
        this.loadList()
      }
      ,
      onSizeChange(val) {
        this.listQuery.pageSize = val
        this.loadList()
      }
      ,
      onCurrentChange(val) {
        this.listQuery.pageNumber = val
        this.loadList()
      },
      /***   公用部分 end ***/
      // 查看登记表
      scanResume(id){
        this.showResumeTemplate = true
        this.resume_dengji_id = id
        var company = this.company;
        var template_id = '';
        if(company.template){
          template_id = company.template
        }else{
          template_id = 'B88888888'
        }
        this.template = template_id
      },
      resumeWinClose(){
        this.showResumeTemplate = false
      },
      // 通知培训
      noticeTrain(){
        var ids = "";
        if (this.multipleSelection.length == 0) {
          Msg.error("请先选择");
          return false;
        } else {
          ids = _.pluck(this.multipleSelection, "id").join(",");
        }
        Http.getModel(API.send_train_notice, {ids: ids}, false).then((json) => {
          if (json.code == 0) {
            Msg.success("操作成功！",()=>{
            })
          } else {
            Msg.error(json.msg)
          }
        })
      },
      // 通知面试
      noticeMianshi(){
        var mobiles = '';
        var ids = "";
        if (this.multipleSelection.length == 0) {
          Msg.error("请先选择");
          return false;
        } else {
          ids = _.pluck(this.multipleSelection, "id").join(",");
          mobiles = _.pluck(this.multipleSelection, "mobile").join(",");
        }
        this.ids = ids;
        this.mobiles = mobiles
        this.showMianshi = true
      },
      noticeWinClose(){
        this.showMianshi = false
      },
      //  导出花名册
      doExportExcel: function (ids) {
        if (!ids) {
          if (this.multipleSelection.length == 0) {
            Msg.error("请先选择");
            return false;
          } else {
             ids = _.pluck(this.multipleSelection, "id").join(",");
          }
        }
        var company = this.company;
        var template_id = '';
        if(company.template){
          template_id = company.template
        }else{
          template_id = 'B88888888'
        }
        var DownLoadFile = function (options) {
          var config = $.extend(true, { method: 'post' }, options);
          var $iframe = $('<iframe id="down-file-iframe" />');
          var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
          $form.attr('action', config.url);
          for (var key in config.data) {
            $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
          }
          $iframe.append($form);
          $(document.body).append($iframe);
          $form[0].submit();
          $iframe.remove();
        }
        var params = {
          url:window.server_root+API.export_worker_excel,
          data:{
            ids:ids,
            template:template_id
          }
        }
        DownLoadFile(params)
        Http.getModel(API.set_huamingce_flag, {ids: ids}, false).then((json) => {
          this.loading = false
          if (json.code == 0) {
            this.loadList()
          }
        })
      },
      //  下载简历
      downloadResume(id){
        var company = this.company;
        var template_id = '';
        if(company.template){
          template_id = company.template
        }else{
          template_id = 'B88888888'
        }
        var DownLoadFile = function (options) {
          var config = $.extend(true, { method: 'post' }, options);
          var $iframe = $('<iframe id="down-file-iframe" />');
          var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
          $form.attr('action', config.url);
          for (var key in config.data) {
            $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
          }
          $iframe.append($form);
          $(document.body).append($iframe);
          $form[0].submit();
          $iframe.remove();
        }
        var params = {
          url:window.server_root+API.batch_export_resumes,
          data:{
            ids:id,
            template:template_id
          }
        }
        DownLoadFile(params)
      },
      //  显示面试情况
      showInterviewWin(id,str){
        this.showInterview = true
        var interview_id = id;
        var detail_array = JSON.parse(str);
        var detail_obj = {};
        for(var i=0;i<detail_array.length;i++){
          if(detail_array[i].interview_id==interview_id){
            detail_obj=detail_array[i];
            break
          }
        }
        this.interview_data = detail_obj
      },
      interviewWinClose(){
        this.showInterview = false
      },


    }
  }
</script>
<style>

</style>
